<template>
  <div class="subbranch-info">
    <div class="container">
         <div class="info-title text-overflow"><span>门店信息</span><el-button @click="back" type="text">返回</el-button></div>
         <div class="btn-box">
             <!-- <el-button style="width:200px;height:40px;" @click="addShop" type="success">添加门店</el-button> -->
         </div>
         <div class="shop-list" v-loading="loading">
             <table border="0" cellspacing="0" v-if="shopList.length !== 0">
                 <thead>
                     <tr>
                         <th><div class="table-cell">门店名称</div></th>
                         <th><div class="table-cell">门店编号</div></th>
                         <th><div class="table-cell">所属商户</div></th>
                         <th><div class="table-cell">商户号</div></th>  
                         <th><div class="table-cell">所在省市</div></th>
                         <th><div class="table-cell">门店地址</div></th>
                         <th><div class="table-cell">联系人</div></th>
                         <th><div class="table-cell">联系电话</div></th>
                         <th><div class="table-cell">创建时间</div></th>
                         <th><div class="table-cell">操作</div></th>
                     </tr>
                 </thead>
                 <tbody>
                     <tr v-for="(cell,index) in shopList" :key="index">
                         
                        <td>
                             <div class="table-cell" v-html="cell.storeName"></div>
                         </td>
                          <td>
                             <div class="table-cell" v-html="cell.storeNo"></div>
                         </td>
                        <td>
                             <div class="table-cell" v-html="cell.shopName"></div>
                         </td>
                         <td>
                             <div class="table-cell" v-html="cell.shopId"></div>
                         </td>

                         <td>
                             <div class="table-cell" v-html="cell.province + cell.city"></div>
                         </td>
                         <td>
                             <div class="table-cell" v-html="cell.adress"></div>
                         </td>
                         <td>
                             <div class="table-cell" v-html="cell.people"></div>
                         </td>
                         <td>
                             <div class="table-cell" v-html="cell.phone"></div>
                         </td>
                         <td>
                             <div class="table-cell" v-html="timestampToTime(cell.createTime)"></div>
                         </td>
                         <td>
                             <div class="table-cell">
                                 <el-button @click="getDetail(cell)" type="text">修改 </el-button>
                             </div>
                         </td>
                     </tr>
                 </tbody>
             </table>
             <div class="no-data" v-else>
                    暂无门店
             </div>
             <div class="pages-control">
                  <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page.sync="pages.currentPage"
                    :page-size="pages.page_size"
                    layout="total, prev, pager, next"
                    :total="pages.total">
                  </el-pagination>
              </div>
         </div>
    </div>

    <!-- 修改门店 -->
    <el-dialog
        title="修改门店"
        :visible.sync="centerDialogVisible"
        center>
        <div class="store-info">
            <el-form :model="info" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
                <el-form-item label="所属商户" prop="shopName">
                    <el-input disabled v-model="info.shopName"></el-input>
                </el-form-item>
                <el-form-item label="门店编号" prop="storeNo">
                    <el-input disabled v-model="info.storeNo"></el-input>
                </el-form-item>
                <el-form-item label="门店名称" prop="storeName">
                    <el-input v-model="info.storeName"></el-input>
                </el-form-item>
                <el-form-item label="所在省份" prop="province">
                    <el-select class="info-select" v-model="info.province" @change="handleShopProv" placeholder="请选择">
                        <el-option
                        v-for="item in provinceList"
                        :key="item.provinceid"
                        :label="item.province"
                        :value="item.province">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所在城市" prop="city">
                    <el-select class="info-select" v-model="info.city" placeholder="请选择">
                        <el-option
                        v-for="item in cityList"
                        :key="item.city"
                        :label="item.city"
                        :value="item.city">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店地址" prop="adress">
                    <el-input v-model="info.adress"></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="people">
                    <el-input v-model="info.people"></el-input>
                </el-form-item>
                <el-form-item label="联系人手机号" prop="phone">
                    <el-input v-model="info.phone"></el-input>
                </el-form-item>
                <el-form-item label="联系人邮箱" prop="email">
                    <el-input v-model="info.email"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitShop">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name:'subbranch-info',
  data () {
    return {
        centerDialogVisible:false,
        loading:false,
        shopList:[],
        provinceList:[],
        cityList:[],
        info:{
            shopId:"",
            province:"",
            city:"",
            adress:"",
            storeName:"",
            people:"",
            phone:"",
            email:"",
            storeNo:""
        },
        rules:{
            storeName: [
                { required: true, message: '门店名称不为空', trigger: 'blur' }
            ],
            people:[
                { required: true, message: '联系人不为空', trigger: 'blur' }
            ],
            phone:[
                { required: true, message: '联系人手机号不为空', trigger: 'blur' }
            ]
        },
        shopId:'',
        pages:{
          total:100,
          page_size:20,
          currentPage:1
        }
    };
  },
  components: {},
  created() {
      this.shopId = this.$route.params.shopId;
      this.queryByStatus();
  },
  mounted() {},
  methods: {
      back(){
          this.$router.back();
      },
       getProvinceList(){
          let url = `user/app/province/queryall`;
          this.$Http.post(url,``).then(res=>{
              if(res.data.resp_code === "000000"){
                  this.provinceList = res.data.result;
              }else{
                   this.$message(res.data.resp_message);
              }
          }).catch(err=>{
              this.$message("获取省份信息失败！");
          })
      },
      handleShopProv(value){
          this.info.shopCity = '';
          this.info.shopAreas = '';
          let target = this.provinceList.find((item)=>{
              return item.province == value;
          });
          let v = null;
          target && (v = target.provinceid);
           let url = `user/app/city/queryall`;
          let params = `provinceid=${v}`;
          this.$Http.post(url,params).then(res=>{
              if(res.data.resp_code === "000000"){
                  this.cityList = res.data.result;
              }else{
                  this.$message(res.data.resp_message);
              }
          }).catch(err=>{
                this.$message("获取列表失败！");
          })

      },
      timestampToTime(timestamp){
        // if(timestamp.length!==13){
        //     return "0000-00-00"
        // }
        var date = new Date(+timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        let D = date.getDate() + ' ';
        let h = date.getHours() + ':';
        let m = date.getMinutes() + ':';
        let s = date.getSeconds();
        return Y+M+D+h+m+s;
      },
      handleCurrentChange(value){
        console.log("当前页为：",value);  
        this.queryByStatus();
      },
      queryByStatus(){
          if(this.loading){
              this.$message("正在加载！请稍候！");
              return;
          }
          let url = `user/store/findByShopId`;
           let params = `?shopId=${this.shopId}&page=${this.pages.currentPage-1}&size=${this.pages.page_size}`; 
            this.loading = true;
            this.$Http.get(url+params).then(res=>{
                this.loading = false;
                if(res.data.resp_code === "000000"){
                    
                    this.shopList = res.data.result.content;
                    this.pages.total = res.data.result.totalElements;
                    if(res.data.result.content.length<=0){
                        this.$notify.info({
                        title: '消息',
                        message: "暂无商户信息！"
                        }); 
                    }
                }else{
                    this.$notify.info({
                        title: '消息',
                        message: res.data.resp_message
                    }); 
                } 
            }).catch(err=>{
                this.loading = false;
                this.$notify.error({
                    title: '错误',
                    message: '获取信息失败！'
                });
            })
      },
      getDetail(cell){
        this.getProvinceList();
        this.info = {...cell};
        this.centerDialogVisible = true;
      },
     
      submitShop(){
        let token = sessionStorage.getItem(1);
        let url = `user/store/updataStoreInfo/${token}`;
        
        let params = [];
        for(let key in this.info){
            params.push(`${key}=${this.info[key]}`);
        }
        params = params.join('&');
        this.$Http.post(url,params).then(res=>{
            if(res.data.resp_code === "000000"){
                this.$notify.success({
                    title:"提示",
                    content:"修改门店信息成功！"
                })
            }else{
                this.$message(res.data.resp_message)
            }
        }).catch(err=>{
            this.$notify.error({
                title: '错误',
                message: '服务器繁忙！修改失败！'
            });
        })

    //    this.info.shopId = this.$route.params.shopId;
    //    let params = JSON.stringify(this.info);
    //    let params = `shopId=${this.$route.params.shopId}&province=${this.info.province}&city=${this.info.city}&adress=${this.info.adress}&storeName=${this.info.storeName}&people=${this.info.people}&phone=${this.info.phone}&email=${this.info.email}`;
    //    this.$Http.post(url,params).then(res=>{
    //         if(res.data.resp_code ==="000000"){
    //             this.centerDialogVisible = false;
    //             this.queryByStatus();
    //         }
    //         this.$message(res.data.resp_message)
    //     }).catch(err=>{
    //         this.$message("请求失败");
    //     })

      }
  }
}

</script>
<style scoped>
.subbranch-info{
    box-sizing: border-box;
    padding: 20px;
}
.container{
  border: 1px solid #ededed;
}
.info-title{
    height: 50px;
  line-height: 50px;
  font-size: 26px;
  color: #409eff;
  padding: 5px 20px;
  border-bottom: 1px solid #ededed;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.filter-input,.filter-select{
  width: 160px;
  margin-right: 10px;
}
.btn-box{
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
}
table{
    background-color: #FFF;
    width: 100%;
    font-size: 12px;
    min-width: 900px;
}
th{
    position: relative;
    word-wrap: normal;
    text-overflow: ellipsis;
    vertical-align: middle;
    border-bottom: 1px solid #ebeef5;
    
}
thead{
    padding: 8px 0;
    color: #909399;
    background-color: #F5F7FA;
}
tr{
    padding: 8px 0;
}
tr:hover{
    background-color: #F5F7FA;
}
td{
    border: none;
    border-bottom: 1px solid #ebeef5;
}
.table-cell{
    white-space: normal;
    word-break: break-all;
    line-height: 22px;
    padding: 10px 10px;
    text-align: center;
    min-width: 40px;
    box-sizing: border-box;
}
.no-data{
    text-align: center;
    padding: 100px ;
    border: 1px solid #dbdbdb;
    color: #dbdbdb;
}

.pages-control{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 40px;
}
</style>